ALTER TABLE "ProgressReportDiet"
ADD COLUMN "IsMorning" boolean ;

ALTER TABLE "ProgressReportDiet"
ADD COLUMN "IsAfternoon" boolean ;
ALTER TABLE "ProgressReportDiet"
ADD COLUMN "IsNight" boolean ;


------------

Create table "RetailWareHouseLink"("RetailWareHouseLinkId" serial Primary key,"RetailPharmacyId" int references "RetailPharmacy"("RetailPharmacyId"),
								  "PharmacyWareHouseId" int references "PharmacyWareHouse"("PharmacyWareHouseId"));
--------


Delete from "PharmacyWareHouseUser";

-------

Delete from "RetailPharmacy" where "RetailPharmacyId" = 3

update "PharmacyStock" set "PharmacyWareHouseId" = 1;

----------

update "PharmacyPurchaseHeader" set "PharmacyWareHouseId" = 1;

Delete from "PharmacyWareHouse" where "PharmacyWareHouseId" <> 1;

---------


Alter table "PharmacyRetailStock" add column "RetailWareHouseLinkId" int 
				references "RetailWareHouseLink"("RetailWareHouseLinkId");
----------------

INSERT INTO public."RetailWareHouseLink"(
	"RetailPharmacyId", "PharmacyWareHouseId")
	VALUES (1,1);
	
INSERT INTO public."RetailWareHouseLink"(
	"RetailPharmacyId", "PharmacyWareHouseId")
	VALUES (2,1);

------
--From Here the query is dependent
Select * from "RetailWareHouseLink";


update "PharmacyRetailStock" set "RetailWareHouseLinkId" = 3
where "RetailPharmacyId" = 1;
update "PharmacyRetailStock" set "RetailWareHouseLinkId" = 4
where "RetailPharmacyId" = 2;

ALter table "RetailPharmacy" drop column "PharmacyWareHouseId";

Alter table "PharmacyRetailStock" drop column "RetailPharmacyId";


-------


insert into "PharmacyLogType"("PharmacyLogTypeId","LogTypeName","Active") values (18,'WareHouse',true);

alter table "IssueHeader" add column "PharmacyWareHouseId" int references "PharmacyWareHouse"("PharmacyWareHouseId");

--------
###
BEGIN

with A as (
select id."IssueHeaderId" as "HeaderIds", pwh."WareHouseName" ,pwh."PharmacyWareHouseId"  from "IssueDetail" id 
join "PharmacyStock" prs on prs."PharmacyStockId" = id."StockId"  
join "PharmacyWareHouse" pwh on pwh."PharmacyWareHouseId"  = prs."PharmacyWareHouseId" 
where id."IssueHeaderId" in (
select "IssueHeaderId" from "IssueHeader" where "IndentHeaderId" in (select "IndentHeaderId"  from "IndentHeader" ih where "IndentTo" = 'P')
)
--group by pwh."WareHouseName" ,pwh."PharmacyWareHouseId"
)
--select split_part(A."HeaderIds",',')::int from A
--select * from A;

update "IssueHeader" set "PharmacyWareHouseId" = A."PharmacyWareHouseId" from A
where "IssueHeaderId"  in (select "HeaderIds" from A);

commit
---------
####

DROP FUNCTION public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer);

CREATE OR REPLACE FUNCTION public."udf_PharmacyBills_Report"(
	"billNumber" text DEFAULT NULL::text,
	"patientId" integer DEFAULT NULL::integer,
	"patientMobile" character varying DEFAULT NULL::text,
	"uMRNo" character varying DEFAULT NULL::text,
	"paidVia" character varying DEFAULT NULL::text,
	"providerId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"retailName" text DEFAULT NULL::text,
	"retailPharmacyId" integer DEFAULT NULL::integer)
    RETURNS TABLE("PharmacySaleHeaderId" integer, "BillNumber" character varying, "SaleDate" timestamp without time zone, "PatientName" character varying, "PatientMobile" character varying, "UMRNo" character varying, "PaidVia" text, "CreatedByName" text, "RoleName" character varying, "ProviderName" character varying, "TotalAmount" numeric, "RetailName" text, "OverallTaxes" numeric) 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$
BEGIN

return query
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."CreatedDate" "SaleDate",PH."PatientName",PH."Mobile" 
,Pa."UMRNo",PH."PaidVia",A."FullName" 
"CreatedByName",R."RoleName" "Role" ,
PH."ProviderName", PH."OverallNetAmount" ,RP."RetailName",PH."OverallTaxes"
from "PharmacySaleHeader" PH
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
--join "Provider" pr on pr."ProviderId"::text=PH."ProviderId"::text
join "Account" A on A."AccountId"=PH."CreatedBy"
join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
	
where	case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and 
case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
				case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" ="providerId"  end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
 case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end and 
 case when "toDate" is null then 1=1 else PH."SaleDate" <= "toDate" end  and
 case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and 
 	case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end  

group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."SaleDate", PH."OverallNetAmount",PH."PatientName",PH."Mobile",Pa."UMRNo",PH."PaidVia",A."FullName" 
,R."RoleName" ,PH."ProviderName",RP."RetailName",PH."OverallTaxes"
order by PH."SaleDate" desc;
END
$BODY$;

ALTER FUNCTION public."udf_PharmacyBills_Report"(text, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer)
    OWNER TO postgres;
-------------


DROP FUNCTION public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean);

CREATE OR REPLACE FUNCTION public."udf_PharmacyBills_FinalReport"(
	"billNumber" text DEFAULT NULL::text,
	"accountId" integer DEFAULT NULL::integer,
	"patientId" integer DEFAULT NULL::integer,
	"patientMobile" character varying DEFAULT NULL::text,
	"uMRNo" character varying DEFAULT NULL::text,
	"paidVia" character varying DEFAULT NULL::text,
	"providerId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"retailName" text DEFAULT NULL::text,
	"retailPharmacyId" integer DEFAULT NULL::integer,
	"pharmacyBillType" boolean DEFAULT NULL::boolean)
    RETURNS TABLE("PharmacySaleHeaderId" integer, "BillNumber" character varying, "PaidVia" text, "SaleDate" timestamp without time zone, "PatientName" character varying, "PatientMobile" character varying, "UMRNo" character varying, "ProviderName" character varying, "CreatedByName" text, "RoleName" character varying, "TotalAmount" numeric, "SaleReturnHeaderId" integer, "OverallTaxes" numeric, "RetailName" text) 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$
BEGIN

return query

select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate" "SaleDate",PH."PatientName",
PH."Mobile" ,Ph."UMRNo",PH."ProviderName",ph."CreatedByName",
ph."Role", PH."OverallNetAmount",PH."SaleReturnHeaderId" ,PH."OverallTaxes",PH."RetailName"
from 
(
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."CreatedDate" "SaleDate",
	PH."PatientName",PH."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role" ,
	PH."OverallNetAmount",true "PharmacyBillType",null "SaleReturnHeaderId",PH."OverallTaxes",
	RP."RetailName"
from "PharmacySaleHeader" PH
	join "Account" A on A."AccountId"=PH."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where	case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and 
	case when "accountId" is null then 1=1 else PH."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
		case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId" end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
    case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end and
	case when "toDate" is null then 1=1 else PH."SaleDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
	case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end  
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate", PH."OverallNetAmount",
	PH."PatientName",PH."ProviderName",Pa."UMRNo",A."FullName" 
,R."RoleName",RP."RetailName"
union 
	
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate" "SaleDate",PH."PatientName",
	Ph."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role", -srh."OverallNetAmount" "TotalAmount",false "PharmacyBillType"
,srh."SaleReturnHeaderId",srh."OverallTaxes",
	RP."RetailName"
	from "SaleReturnHeader" srh
Join "PharmacySaleHeader" ph on ph."PharmacySaleHeaderId"= srh."PharmacySaleHeaderId"
	join "Account" A on A."AccountId"=srh."CreatedBy"
		join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end  and
	case when "accountId" is null then 1=1 else srh."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
			case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId"  end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
 case when "fromDate" is null then 1=1 else srh."ReturnDate" >= "fromDate" end and 
	case when "toDate" is null then 1=1 else srh."ReturnDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
		case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end  
	
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate", srh."OverallNetAmount",
	PH."PatientName",Pa."Mobile",Pa."UMRNo",PH."ProviderName",A."FullName" 
,R."RoleName",srh."SaleReturnHeaderId", RP."RetailName"
) Ph
where case when "pharmacyBillType" is null then 1=1 
when "pharmacyBillType" = true then "PharmacyBillType" = true
when "pharmacyBillType" = false then "PharmacyBillType" = false
end 
order by PH."SaleDate" desc, PH."PharmacySaleHeaderId";
END
$BODY$;

ALTER FUNCTION public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean)
    OWNER TO postgres;

--------------------------------

-- FUNCTION: public.udf_PharmacyBills_FinalReport(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean)

DROP FUNCTION IF EXISTS public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean);

CREATE OR REPLACE FUNCTION public."udf_PharmacyBills_FinalReport"(
	"billNumber" text DEFAULT NULL::text,
	"accountId" integer DEFAULT NULL::integer,
	"patientId" integer DEFAULT NULL::integer,
	"patientMobile" character varying DEFAULT NULL::text,
	"uMRNo" character varying DEFAULT NULL::text,
	"paidVia" character varying DEFAULT NULL::text,
	"providerId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"retailName" text DEFAULT NULL::text,
	"retailPharmacyId" integer DEFAULT NULL::integer,
	"pharmacyBillType" boolean DEFAULT NULL::boolean)
    RETURNS TABLE("PharmacySaleHeaderId" integer, "BillNumber" character varying, "PaidVia" text, "SaleDate" timestamp without time zone, "PatientName" character varying, "PatientMobile" character varying, "UMRNo" character varying, "ProviderName" character varying, "CreatedByName" text, "RoleName" character varying, "TotalAmount" numeric, "SaleReturnHeaderId" integer, "OverallTaxes" numeric, "RetailName" text) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query

select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate" "SaleDate",PH."PatientName",
PH."Mobile" ,Ph."UMRNo",PH."ProviderName",ph."CreatedByName",
ph."Role", PH."OverallNetAmount",PH."SaleReturnHeaderId" ,PH."OverallTaxes",PH."RetailName"
from 
(
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."CreatedDate" "SaleDate",
	PH."PatientName",PH."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role" ,
	PH."OverallNetAmount",true "PharmacyBillType",null "SaleReturnHeaderId",PH."OverallTaxes",
	RP."RetailName"
from "PharmacySaleHeader" PH
	join "Account" A on A."AccountId"=PH."CreatedBy"
	join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where	case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end and 
	case when "accountId" is null then 1=1 else PH."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
		case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId" end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
    case when "fromDate" is null then 1=1 else PH."SaleDate" >= "fromDate" end and
	case when "toDate" is null then 1=1 else PH."SaleDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
	case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end  
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",PH."SaleDate", PH."OverallNetAmount",
	PH."PatientName",PH."ProviderName",Pa."UMRNo",A."FullName" 
,R."RoleName",RP."RetailName"
union 
	
select PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate" "SaleDate",PH."PatientName",
	Ph."Mobile" ,Pa."UMRNo",PH."ProviderName",A."FullName" 
"CreatedByName",R."RoleName" "Role", -srh."OverallNetAmount" "TotalAmount",false "PharmacyBillType"
,srh."SaleReturnHeaderId",srh."OverallTaxes",
	RP."RetailName"
	from "SaleReturnHeader" srh
Join "PharmacySaleHeader" ph on ph."PharmacySaleHeaderId"= srh."PharmacySaleHeaderId"
	join "Account" A on A."AccountId"=srh."CreatedBy"
		join "Role" R on R."RoleId"=A."RoleId"
	join "PharmacySaleDetail" PSD on PSD."PharmacySaleHeaderId"=PH."PharmacySaleHeaderId"
	join "PharmacyRetailStock" PRS on PRS."PharmacyRetailStockId"=PSD."PharmacyRetailStockId"
	join "RetailWareHouseLink" RWL on RWL."RetailWareHouseLinkId" = PRS."RetailWareHouseLinkId"
	join "RetailPharmacy" RP on RP."RetailPharmacyId"=RWL."RetailPharmacyId"
left join "Patient" Pa on Pa."PatientId"::text=PH."PatientId"::text
		
where case when "billNumber" is null then 1=1 else  PH."BillNumber" ilike '%' || "billNumber" ||'%' end  and
	case when "accountId" is null then 1=1 else srh."CreatedBy"="accountId" end and
	case when "patientId" is null then 1=1 else PH."PatientId"="patientId" end and
	case when "patientMobile" is null then 1=1 else  PH."Mobile" ilike '%' || "patientMobile" ||'%' end  and
	case when "uMRNo" is null then 1=1 else  Pa."UMRNo" ilike '%' || "uMRNo" ||'%' end  and
			case when "paidVia" is null then 1=1 else  PH."PaidVia" ilike '%' || "paidVia" ||'%' end  and
	case when "providerId" is null then 1=1 else  PH."ProviderId" = "providerId"  end  and
		case when "createdBy" is null then 1=1 else  A."AccountId"="createdBy" end and
 case when "fromDate" is null then 1=1 else srh."ReturnDate" >= "fromDate" end and 
	case when "toDate" is null then 1=1 else srh."ReturnDate" <= "toDate" end and
	case when "retailName" is null then 1=1 else  RP."RetailName" ilike '%' || "retailName" ||'%' end and
		case when "retailPharmacyId" is null then 1=1 else  RP."RetailPharmacyId" = "retailPharmacyId" end  
	
group by PH."PharmacySaleHeaderId" ,PH."BillNumber",PH."PaidVia",srh."ReturnDate", srh."OverallNetAmount",
	PH."PatientName",Pa."Mobile",Pa."UMRNo",PH."ProviderName",A."FullName" 
,R."RoleName",srh."SaleReturnHeaderId", RP."RetailName"
) Ph
where case when "pharmacyBillType" is null then 1=1 
when "pharmacyBillType" = true then "PharmacyBillType" = true
when "pharmacyBillType" = false then "PharmacyBillType" = false
end 
order by PH."SaleDate" desc, PH."PharmacySaleHeaderId";
END
$BODY$;

ALTER FUNCTION public."udf_PharmacyBills_FinalReport"(text, integer, integer, character varying, character varying, character varying, integer, integer, timestamp without time zone, timestamp without time zone, text, integer, boolean)
    OWNER TO postgres;
